namespace Brjnk.GpxExplorer.Workspace.GCBrowser
{
    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.Linq;
    using Brjnk.Common;
    using Brjnk.GpxExplorer.Data;
    using Brjnk.GpxExplorer.UI;

    /// <summary>
    /// </summary>
    public class GcBrowser : Browser
    {
        private DataModel data;

        public GcBrowser(DataModel data)
        {
            this.data = data;
        }

        public override BrowserTab OpenNewTab(Uri uri, bool focusTab)
        {
            var tab = new GcBrowserTab(this);
            tab.Navigate(uri);
            Tabs.Add(tab);
            if (focusTab) tab.IsActive = true;
            return tab;
        }

        public override BrowserTab OpenNewTab(bool focus)
        {
            return OpenNewTab(new Uri("http://geocaching.com"), true);
        }

        public override BrowserTab OpenHomeTab()
        {
            return OpenNewTab(new Uri("http://geocaching.com"), true);
        }

        public bool ContainsGc(string gcCode)
        {
            return data.Waypoints.Any(w => w.Source.Name.Equals(gcCode, StringComparison.InvariantCultureIgnoreCase));
        }

        public void ImportLoaded()
        {
            List<ImportAllItem> items = new List<ImportAllItem>();
            foreach (var i in Loaded)
                items.Add(new ImportAllItem(i, GetImportType(i)));

            var window = new ImportAllGCsWindow(items);
            if (window.ShowDialog() ?? false)
            {
                foreach (var i in items)
                {
                    if (i.IsSelected)
                    {
                        Import(i.Geocache);
                        Loaded.Remove(i.Geocache);
                    }
                }
            }
        }

        private ImportType GetImportType(Geocache i)
        {
            if (ContainsGc(i.GcCode)) return ImportType.Update;
            return ImportType.Import;
        }

        public void Import(Geocache geocache)
        {
            var wpt = data.Waypoints.FirstOrDefault(w => w.Source.Name.Equals(geocache.GcCode, StringComparison.InvariantCultureIgnoreCase));
            if (wpt != null)
            {
                wpt.Source.Position = geocache.Coordinates;
                wpt.Source.Geocache = geocache.Info;
                wpt.Source.Description = geocache.Info.Name;
                wpt.Source.Symbol = geocache.Found ? WaypointSymbol.Geocache_Found : WaypointSymbol.Geocache;
            }
            else
            {
                var wptData = new WaypointDefault();
                wptData.Name = geocache.GcCode;
                wptData.Position = geocache.Coordinates;
                wptData.Geocache = geocache.Info;
                wptData.Description = geocache.Info.Name;
                wptData.Symbol = geocache.Found ? WaypointSymbol.Geocache_Found : WaypointSymbol.Geocache;
                wpt = new ViewModel.WaypointVM(wptData);
                data.Waypoints.Add(wpt);
            }

            //odebrat z kolekce Loaded
            var toRemove = Loaded.FirstOrDefault(g => g.GcCode.Equals(geocache.GcCode, StringComparison.InvariantCultureIgnoreCase));
            Loaded.Remove(toRemove);
        }

        public void AddLoaded(Geocache gc)
        {
            var old = Loaded.FirstOrDefault(g => g.GcCode.Equals(gc.GcCode, StringComparison.InvariantCultureIgnoreCase));
            Loaded.Remove(old);
            Loaded.Add(gc);
        }

        private ObservableCollection<Geocache> loaded = new ObservableCollection<Geocache>();

        public ObservableCollection<Geocache> Loaded { get { return loaded; } }

        public GenericCommand ImportLoadedCommand { get; private set; }

        protected override void CreateCommands()
        {
            base.CreateCommands();
            ImportLoadedCommand = new GenericCommand(o => ImportLoaded());
        }
    }
}